package top.linyuxb.lycloud.config.starter.event;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import top.linyuxb.lycloud.common.properties.LyConfigProperties;
import top.linyuxb.lycloud.config.starter.thread.LyConfigThread;
import top.linyuxb.lycloud.config.starter.thread.LyServerThread;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/5/28 18:26
 * @Desc: 服务启动监听
 */
@Slf4j
@Component
public class MyStartupListener implements ApplicationListener<ApplicationReadyEvent>, DisposableBean {
    @Resource
    private LyConfigProperties lyRpcClientProperties;
    @Value(value = "${server.port:8080}")
    private String port;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("MyStartupListener onApplicationEvent start");
        LyConfigThread.getInstance().start(lyRpcClientProperties);
        LyServerThread.getInstance().start(lyRpcClientProperties, port);
        log.info("MyStartupListener onApplicationEvent end");
    }

    @Override
    public void destroy() throws Exception {
        log.info("MyStartupListener destroy start");
        LyConfigThread.getInstance().toStop();
        LyServerThread.getInstance().toStop();
        log.info("MyStartupListener destroy start");
    }
}